<?php
class WorthlessShare extends AppModel {
	var $name = 'WorthlessShare';
	var $useDbConfig = 'ws';
	var $primaryKey = 'LineNo';
	var $displayField = 'RegdName1';
        var $order = "CommonID DESC";
	var $validate = array(
		'CompanyName' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'CompanyCode' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'RegdName1' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
        
        
        
    function import($filename) {
        $error = false;

        // open the file
        $handle = fopen($filename, "r");

        // read the 1st row as headings
        $header = fgetcsv($handle);

        // create a message container
        $return = array(
            'messages' => array(),
            'errors' => array(),
        );
        $i = 0;
        // read each data row in the file
        while (($row = fgetcsv($handle)) !== FALSE) {
            $i++;
            $data = array();

            // for each header field 
            foreach ($header as $k => $head) {
                // get the data field from Model.field
                //echo $head;exit;
                if (strpos($head, '.') !== false) {
                    $h = explode('.', $head);
                    $data[$h[0]][$h[1]] = (isset($row[$k])) ? $row[$k] : '';
                }
                // get the data field from field
                else {
                    $data['WorthlessShare'][$head] = (isset($row[$k])) ? $row[$k] : '';
                }
            }

            // see if we have an id 			
            $id = isset($data['WorthlessShare']['id']) ? $data['Director']['id'] : 0;

            // we have an id, so we update
            if ($id) {
                // there is 2 options here, 
                // option 1:
                // load the current row, and merge it with the new data
                //$this->recursive = -1;
                //$post = $this->read(null,$id);
                //$data['Post'] = array_merge($post['Post'],$data['Post']);
                // option 2:
                // set the model id
                $this->id = $id;
            }
            // or create a new record
            else {
                $this->create();
            }

            // see what we have
            debug($data);exit;
            //pr($data);
            // validate the row
            $this->set($data);
            if (!$this->validates()) {
                //$this->_flash('warning');
                $return['errors'][] = __(sprintf('WorthlessShare for Row %d failed to validate.', $i), true);
                $error = true;
            }
            //debug($this->data);exit;
            // save the row
            if (!$error && !$this->save($data)) {
                $return['errors'][] = __(sprintf('WorthlessShare for Row %d failed to save.', $i), true);
                $error = true;
            }

            // success message!
            if (!$error) {
                $return['messages'][] = __(sprintf('WorthlessShare for Row %d was saved.', $i), true);
            }
            //if($i==10){echo"sahabj"; break;}
        }

        // close the file
        fclose($handle);

        // return the messages
        return $return;
    }
}
